package com.tiger.weightcheck;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 数据存储
 * 
 * @author chenlin
 * 
 */
public class PreferenceDataUtil {
	public final String CHECKRESULT="checkresult";
	private static final String PREFERENCE_NAME="weightcheck";
	private Context context;

	public PreferenceDataUtil(Context context) {
		synchronized (this) {
			this.context = context;
		}
	}

	public SharedPreferences getPreference(){
		SharedPreferences store = this.context.getSharedPreferences(
				PREFERENCE_NAME, 0);
		return store;
	}
	/**
	 * 读取数据
	 * 
	 * @param filed
	 * @return
	 */
	public String ReadSharedPreferences(String filed, String defaultValue) {
		SharedPreferences store = this.context.getSharedPreferences(
				PREFERENCE_NAME, 0);
		String strName = store.getString(filed, defaultValue);
		return strName;
	}
	public boolean ReadSharedPreferences(String filed, boolean defaultValue) {
		SharedPreferences store = this.context.getSharedPreferences(
				PREFERENCE_NAME, 0);
		boolean strName =store.getBoolean(filed, defaultValue);
		return strName;
	}
	/**
	 * 保存数据
	 * 
	 * @param fields
	 * @param values
	 */
	public void WriteSharedPreferences(String[] fields, String[] values) {
		if (fields == null || values == null)
			return;
		if (fields.length != values.length)
			return;
		SharedPreferences mm = this.context.getSharedPreferences(
				PREFERENCE_NAME, 0);
		SharedPreferences.Editor editor = mm.edit();
		for (int i = 0; i < fields.length; i++) {
			editor.putString(fields[i], values[i]);
		}
		editor.commit();
	}
	public void WriteSharedPreferences(String fields, boolean value) {
		if (fields == null)
			return;
		SharedPreferences mm = this.context.getSharedPreferences(
				PREFERENCE_NAME, 0);
		SharedPreferences.Editor editor = mm.edit();
		editor.putBoolean(fields, value);
		editor.commit();
	}
	public void WriteSharedPreferences(String fields, String value) {
		if (fields == null || value == null)
			return;
		SharedPreferences mm = this.context.getSharedPreferences(
				PREFERENCE_NAME, 0);
		SharedPreferences.Editor editor = mm.edit();
		editor.putString(fields, value);
		editor.commit();
	}
}
